<template>
    <div id="AutoConfiguration">
        <!-- 头部功能区 -->
        <el-row>
            <el-form size="mini" inline :model="query" label-width="80px">
                <!-- 新增搜索 -->
                <el-form-item label="任务名称">
                    <el-input v-model="query.taskName" placeholder="请输入任务名称" />
                </el-form-item>
                <el-form-item label="巡检方式">
                    <el-select v-model="query.flag" placeholder="请选择巡检方式">
                        <el-option label="自动" value="1"></el-option>
                        <el-option label="手动" value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" size="mini" @click="getList()">搜索</el-button>
                    <el-button type="warning" size="mini" @click="resize()">重置</el-button>
                </el-form-item>
            </el-form>
            <el-button class="mb20" type="primary" size="mini" @click="showAdd()">添加配置</el-button>
        </el-row>

        <!-- 列表 -->
        <div class="tp_box">
            <el-table :data="tableData" style="width: 100%" height="calc(100vh - 280px)" :cell-style="cellStyle"
                :default-sort="{ prop: 'date', order: 'descending' }">
                <el-table-column show-overflow-tooltip="ture" prop="taskName" label="任务名称" align="center">
                </el-table-column>
                <el-table-column show-overflow-tooltip="ture" prop="flag" align="center" label="巡检方式">
                    <template slot-scope="{ row: { flag } }">
                        <el-tag type="danger" v-if="+flag == 0">手动</el-tag>
                        <el-tag type="sunness" v-else-if="+flag == 1">自动</el-tag>
                    </template>
                </el-table-column>
                <el-table-column show-overflow-tooltip="ture" prop="hostIp" align="center" label="巡检对象">
                </el-table-column>
                <el-table-column show-overflow-tooltip="ture" prop="executionTime" align="center" label="执行时间">
                </el-table-column>
                <el-table-column show-overflow-tooltip="ture" prop="executionCycle" align="center" label="执行周期">
                    <template slot-scope="scope">
                        <el-tag type="primary">{{
                            scope.row.executionCycle | executionCycle
                        }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" @click="showEdit(scope.row)" size="small">编辑</el-button>
                        <el-button type="danger" size="small" @click="delAction(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="this.total" :page.sync="query.pageNum" :limit.sync="query.pageSize"
                @pagination="getList()" />
        </div>

        <!-- 添加配置弹窗 -->
        <el-dialog title="添加配置" :visible.sync="dialogVisible" width="40%" @close="closeDialog('form')">
            <div class="form_box">
                <el-form :rules="rules" ref="form" label-width="80px" :model="form" size="mini">
                    <el-form-item label="任务名称" prop="taskName">
                        <el-input v-model="form.taskName" placeholder="请输入任务名称" style="width: 200px"></el-input>
                    </el-form-item>
                    <el-form-item label="巡检对象" prop="hostId">
                        <el-select v-model="form.hostId" placeholder="请选择巡检对象">
                            <el-option v-for="item in hosts" :key="item.hostid" :label="item.host"
                                :value="item.hostid"></el-option>
                        </el-select>
                    </el-form-item>
                    <!-- <el-form-item label="巡检对象" prop="object">
            <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll"
              @change="handleCheckAllChange">全选</el-checkbox>
            <div style="margin: 15px 0"></div>
            <el-checkbox-group v-model="form.checkedHosts" @change="handleCheckedHostsChange">
              <el-checkbox v-for="host in hosts" :label="host" :key="host">{{
                host
              }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item> -->
                    <!-- <el-form-item label="监控项" prop="monitor">
            <el-select v-model="form.monitor" multiple placeholder="请选择监控项" style="width: 350px">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
            <div class="add_box mt10">
              <el-button type="primary" size="mini" @click="addMonitorVisible = !addMonitorVisible">自定义监控项</el-button>
              <div class="addMonitor" v-if="addMonitorVisible">
                <el-form label-width="80px" ref="addMonitorForm" :model="addMonitorForm" size="mini">
                  <el-form-item label="任务名称" prop="name">
                    <el-input v-model="addMonitorForm.name" placeholder="监控项唯一标识" style="width: 200px"></el-input>
                  </el-form-item>
                  <el-form-item label="键值" prop="key">
                    <el-input v-model="addMonitorForm.key" placeholder="设定监控的指标" style="width: 200px"></el-input>
                  </el-form-item>
                  <el-form-item label="阈值" prop="valve">
                    <el-input v-model="addMonitorForm.valve" placeholder="请输入阈值" style="width: 200px"></el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="addMonitor">添加</el-button>
                    <el-button @click="addMonitorVisible = false">取消</el-button>
                  </el-form-item>
                </el-form>
              </div>
            </div>
          </el-form-item> -->
                    <el-form-item label="巡检方式" prop="flag">
                        <el-select v-model="form.flag" placeholder="请选择巡检方式">
                            <el-option label="手动执行" value="0"></el-option>
                            <el-option label="自动执行" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="执行时间" prop="executionTime">
                        <!-- 手动执行出现 -->
                        <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="form.executionTime" type="datetime"
                            placeholder="请选择执行日期时间">
                        </el-date-picker>
                    </el-form-item>
                    <!-- 定时执行出现 -->
                    <el-form-item label="执行周期" prop="executionCycle" v-if="form.flag == '1'">
                        <el-select v-model="form.executionCycle" placeholder="请选择执行周期" :disabled="form.flag == '0'">
                            <el-option label="每日" value="1"></el-option>
                            <el-option label="每周" value="2"></el-option>
                            <el-option label="每月" value="3"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm('form')" size="mini">添 加</el-button>
                <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
            </span>
        </el-dialog>

        <!-- 编辑配置弹窗 -->
        <el-dialog title="编辑配置" :visible.sync="editDialogVisible" width="40%" @close="closeDialog('form')">
            <div class="form_box">
                <el-form :rules="rules" ref="editForm" label-width="80px" :model="editForm" size="mini">
                    <el-form-item label="任务名称" prop="taskName">
                        <el-input v-model="editForm.taskName" placeholder="请输入任务名称" style="width: 200px"></el-input>
                    </el-form-item>
                    <el-form-item label="巡检对象" prop="hostId">
                        <el-select v-model="editForm.hostId" placeholder="请选择巡检对象">
                            <el-option v-for="item in hosts" :key="item.hostid" :label="item.host"
                                :value="item.hostid"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="巡检方式" prop="flag">
                        <el-select v-model="editForm.flag" placeholder="请选择巡检方式">
                            <el-option label="手动执行" :value="0"></el-option>
                            <el-option label="自动执行" :value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="执行时间" prop="executionTime">
                        <!-- 手动执行出现 -->
                        <el-date-picker value-format="yyyy-MM-dd hh:mm:ss" v-model="editForm.executionTime" type="datetime"
                            placeholder="请选择执行日期时间">
                        </el-date-picker>
                    </el-form-item>
                    <!-- 定时执行出现 -->
                    <el-form-item label="执行周期" prop="executionCycle" v-if="editForm.flag == '1'">
                        <el-select v-model="editForm.executionCycle" placeholder="请选择执行周期" :disabled="editForm.flag == '0'">
                            <el-option label="每日" value="1"></el-option>
                            <el-option label="每周" value="2"></el-option>
                            <el-option label="每月" value="3"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="editAction('editForm')" size="mini">添 加</el-button>
                <el-button @click="editDialogVisible = false" size="mini">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import {
    getHostSearch_Api, //获取主机列表
} from "@/api/automated/host";
import {
    getIntelligentSmartAddApi,
    getIntelligentSmartListApi,
    getIntelligentSmartUpdateApi,
    getDelIntelligentSmartApi,
} from "@/api/automated/AutomaticConfiguration";
import { editAction_Api } from "../../../api/automated/selfHealing";
export default {
    data() {
        return {
            editForm: {},
            editDialogVisible: false,
            rules: {
                taskName: [
                    { required: true, message: "请输入任务名称", trigger: "blur" },
                ],
                hostId: [
                    { required: true, message: "请选择巡检对象", trigger: "change" },
                ],
                flag: [
                    { required: true, message: "请选择巡检方式", trigger: "change" },
                ],
                executionTime: [
                    { required: true, message: "请选择执行时间", trigger: "change" },
                ],
                executionCycle: [
                    { required: true, message: "请选择执行周期", trigger: "change" },
                ],
            },
            //列表数据
            tableData: [],
            //主机巡检表单
            form: {
                taskName: null, //巡检名称
                hostId: "",
                hostIp: "",
                // checkedHosts: [], //选中的巡检对象
                // monitor: [], //监控项
                flag: "1", //巡检方式
                executionTime: "", //执行时间
                executionCycle: "", //执行周期
            },
            //自定义监控项表单
            addMonitorForm: {
                name: null, //名称
                key: null, //键值
                valve: null, //阈值
            },
            //控制自定义监控项弹窗
            addMonitorVisible: false,
            //添加配置弹窗
            dialogVisible: false,
            checkAll: false, //全选
            hosts: [],
            //用以表示 checkbox 的不确定状态，一般用于实现全选的效果
            isIndeterminate: true,
            //启动方式
            options: [
                {
                    value: 1,
                    label: "CPU使用率",
                },
                {
                    value: 2,
                    label: "内存使用率",
                },
                {
                    value: 3,
                    label: "根分区使用率",
                },
                {
                    value: 4,
                    label: "数据分区使用率",
                },
                {
                    value: 5,
                    label: "运行时间",
                },
                {
                    value: 6,
                    label: "平均负载",
                },
                {
                    value: 7,
                    label: "进程",
                },
                {
                    value: 8,
                    label: "应用",
                },
                {
                    value: 9,
                    label: "客户端",
                },
            ],
            query: {
                pageSize: "10",
                pageNum: "1",
                flag: "",
                taskName: "",
            },
            total: "",
        };
    },
    methods: {
        async getList() {
            let res = await getIntelligentSmartListApi(this.query);
            let { code, data } = res;
            if (code == 200) {
                this.tableData = data.date;
                this.total = data.total;
            }
        },
        //获取主机列表数据
        async getHostList() {
            getHostSearch_Api(this.query).then((res) => {
                this.hosts = res.rows;
            });
        },
        //展示新增
        showAdd() {
            (this.form = {
                taskName: null, //巡检名称
                hostId: "",
                hostIp: "",
                // checkedHosts: [], //选中的巡检对象
                // monitor: [], //监控项
                flag: "1", //巡检方式
                executionTime: "", //执行时间
                executionCycle: "", //执行周期
            }),
                (this.dialogVisible = true);
        },
        //新增
        submitForm(formName) {
            this.hosts.forEach((item) => {
                if (this.form.hostId == item.hostid) {
                    this.form.hostIp = item.ip;
                }
            });
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    let res = await getIntelligentSmartAddApi(this.form);
                    let { code, msg } = res;
                    if (code == 200) {
                        this.$message.success(msg);
                        this.dialogVisible = false;
                        this.getList();
                    } else {
                        this.$message.error(msg);
                    }
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        //展示修改
        showEdit(row) {
            this.editForm = { ...row };
            this.editDialogVisible = true;
        },
        //修改
        async editAction() {
            this.hosts.forEach((item) => {
                if (this.editForm.hostId == item.hostid) {
                    this.editForm.hostIp = item.ip;
                }
            });
            let res = await getIntelligentSmartUpdateApi(this.editForm);
            let { code, msg } = res;
            if (code == 200) {
                this.$message.success(msg);
                (this.editDialogVisible = false), this.getList();
            } else {
                this.$message.error(msg);
            }
        },
        //删除
        delAction(row) {
            console.log(row);
            this.$confirm("此操作将永久删除, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(async () => {
                let res = await getDelIntelligentSmartApi(row.smartId);
                let { code, msg } = res;
                if (code == 200) {
                    this.$message.success(msg);
                    this.getList();
                } else {
                    this.$message.error(msg);
                }
            });
        },
        resize() {
            this.query.taskName = "";
            this.query.flag = "";
            this.getList();
        },
        //关闭弹窗
        closeDialog(formName) {
            this.$refs[formName].resetFields();
        },
        //巡检对象全选
        handleCheckAllChange(val) {
            this.form.checkedHosts = val ? this.hosts : [];
            this.isIndeterminate = false;
        },
        //巡检对象多选
        handleCheckedHostsChange(value) {
            //获取选择的个数
            let checkedCount = value.length;
            //全部选中就点亮全选按钮
            this.checkAll = checkedCount === this.hosts.length;
            this.isIndeterminate =
                checkedCount > 0 && checkedCount < this.hosts.length;
        },
        //自定义监控项添加
        // addMonitor() {
        //   this.form.monitor.push(this.addMonitorForm.name);
        //   this.addMonitorVisible = false;
        // },
    },
    created() {
        this.getList();
        this.getHostList();
    },
    computed: {},
    filters: {
        executionCycle(val) {
            switch (val) {
                case "1":
                    return "每日";
                case "2":
                    return "每周";
                case "3":
                    return "每月";
                case "0":
                    return "单次";
            }
        },
    },
};
</script>
<style lang="scss" scoped>
//自定义监控项样式
.add_box {
    position: relative;

    .addMonitor {
        position: absolute;
        left: 0;
        top: 35px;
        width: 300px;
        box-shadow: 0 0 3px 1px #cccaca;
        border-radius: 5px;
        background-color: #ffffff;
        z-index: 999;
        padding: 5px;
    }
}
</style>
